package com.px.patient.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.px.common.annotation.Log;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.core.page.TableDataInfo;
import com.px.common.enums.BusinessType;
import com.px.common.utils.PatientBaseUtil;
import com.px.patient.domain.PatientAppointment;
import com.px.patient.domain.PatientFollowUp;
import com.px.patient.dto.PatientAppointmentDto;
import com.px.patient.service.IPatientAppointmentService;
import com.px.patient.service.IPatientFollowUpService;
import com.px.patient.vo.PatientFollowUpVO;
import com.px.system.utils.UserCacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 患者回访Controller
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Api(tags = "患者回访")
@RestController
@RequestMapping("/patient-follow-up")
public class PatientFollowUpController extends BaseController {

    @Resource
    private IPatientFollowUpService patientFollowUpService;
    @Resource
    private IPatientAppointmentService patientAppointmentService;

    @GetMapping("/appointment/page")
    @ApiOperation("就诊列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientName", value = "患者姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "medicalRecordNum", value = "用户编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "appointmentProject", value = "预约项目", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "attendingDoctor", value = "主治医生", dataType = "String", dataTypeClass = String.class),
    })
    public TableDataInfo appointmentPage(@RequestParam Map<String, String> map) {
        startPage();
        if (CollectionUtils.isEmpty(map)) {
            map = new HashMap<>();
        }
        map.put("followUpCount", "0");
        List<PatientAppointmentDto> list = patientFollowUpService.findAppointmentLists(map);
        return getDataTable(list);
    }

    /**
     * 回访列表
     */
    @GetMapping("/page")
    @ApiOperation("回访列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientName", value = "患者姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "followUpStatus", value = "回访状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "followUpUser", value = "回访人", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "followUpStartTime", value = "回访搜索开始时间", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "followUpEndTime", value = "回访搜索结束时间", dataType = "String", dataTypeClass = String.class),
    })
    public TableDataInfo page(@RequestParam Map<String, String> map) {
        startPage();
        if (CollectionUtils.isEmpty(map)) {
            map = new HashMap<>();
        }
        List<PatientFollowUpVO> list = patientFollowUpService.findFollowUpLists(map);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(li -> {
                try {
                    li.setAge(PatientBaseUtil.findAge(li.getIdentificationCard()));
                    li.setIdentificationCard(PatientBaseUtil.idCardMask(li.getIdentificationCard()));
                } catch (Exception ignored) {
                }
            });
        }
        return getDataTable(list);
    }

    @GetMapping("/list")
    @ApiOperation("回访列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appointmentId", value = "预约id", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult list(@RequestParam(name = "appointmentId") String appointmentId) {
        List<PatientFollowUp> list = patientFollowUpService.list(Wrappers.<PatientFollowUp>lambdaQuery()
                .eq(PatientFollowUp::getPatientAppointmentId, appointmentId)
                .orderByDesc(PatientFollowUp::getFollowUpTime));
        return success(list);
    }

    /**
     * 新增回访
     */
    @Log(title = "新增回访", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增回访")
    public AjaxResult add(@Valid @RequestBody PatientFollowUp entry) {
        String patientAppointmentId = entry.getPatientAppointmentId();
        PatientAppointment appointment = patientAppointmentService.getById(patientAppointmentId);
        if (Objects.isNull(appointment)) {
            return error("预约不存在");
        }
        entry.setFollowUpUser(UserCacheUtil.getSysUserById(entry.getFollowUpUserId()).getNickName());
        entry.setAppointmentStartTime(appointment.getAppointmentStartTime());
        return toAjax(patientFollowUpService.save(entry));
    }

    /**
     * 修改回访
     */
    @Log(title = "修改回访", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改回访")
    public AjaxResult edit(@Valid @RequestBody PatientFollowUp entry) {
        if (StringUtils.isEmpty(entry.getId())) {
            return error();
        }
        entry.setFollowUpUser(UserCacheUtil.getSysUserById(entry.getFollowUpUserId()).getNickName());
        return success(patientFollowUpService.updateById(entry));
    }

}
